package com.lxt.controller;

import com.lxt.common.HttpResult;
import com.lxt.common.PagedGridResult;
import com.lxt.pojo.Items;
import com.lxt.pojo.ItemsImg;
import com.lxt.pojo.ItemsParam;
import com.lxt.pojo.ItemsSpec;
import com.lxt.pojo.VO.CommentLevelCountsVO;
import com.lxt.pojo.VO.ItemInfoVO;
import com.lxt.pojo.VO.ShopCartVO;
import com.lxt.service.ItemsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "商品接口", tags = {"商品信息展示的相关接口"})
@RestController
@RequestMapping("items")
public class ItemsController extends BaseController {

    final static Logger logger = LoggerFactory.getLogger(ItemsController.class);

    @Autowired
    private ItemsService itemsService;

    @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public HttpResult subCat(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @PathVariable String itemId) {
        if (itemId == null) {
            return HttpResult.errorMsg("商品不存在");
        }

        Items item = itemsService.queryItemById(itemId);
        List<ItemsImg> itemsImg = itemsService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecs = itemsService.queryItemSpecList(itemId);
        ItemsParam itemsParam = itemsService.queryItemParam(itemId);
        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(item);
        itemInfoVO.setItemImgList(itemsImg);
        itemInfoVO.setItemSpecList(itemsSpecs);
        itemInfoVO.setItemParams(itemsParam);
        return HttpResult.ok(itemInfoVO);
    }

    @ApiOperation(value = "查询商品评价等级", notes = "查询商品评价等级", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public HttpResult commentLevel(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId) {
        if (itemId == null) {
            return HttpResult.errorMsg("商品不存在");
        }
        CommentLevelCountsVO countsVO = itemsService.queryCommentCounts(itemId);
        return HttpResult.ok(countsVO);
    }

    @ApiOperation(value = "查询商品评价", notes = "查询商品评价", httpMethod = "GET")
    @GetMapping("/comments")
    public HttpResult queryPagedComments(
            @ApiParam(name = "itemId", value = "商品id", required = true) @RequestParam String itemId,
            @ApiParam(name = "level", value = "评价等级", required = false) @RequestParam Integer level,
            @ApiParam(name = "page", value = "查询第几页", required = false) @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页每一页显示的条数", required = false) @RequestParam Integer pageSize) {
        if (itemId == null) {
            return HttpResult.errorMsg("商品不存在");
        }
        if (page == null) {
            page = COMMENT_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        PagedGridResult result = itemsService.queryPagedComments(itemId, level, page, pageSize);
        return HttpResult.ok(result);
    }


    @ApiOperation(value = "搜索商品列表", notes = "搜索商品列表", httpMethod = "GET")
    @GetMapping("/search")
    public HttpResult searchItems(
            @ApiParam(name = "keywords", value = "关键值", required = true) @RequestParam String keywords,
            @ApiParam(name = "sort", value = "排序规则", required = false) @RequestParam String sort,
            @ApiParam(name = "page", value = "查询第几页", required = false) @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页每一页显示的条数", required = false) @RequestParam Integer pageSize) {
        if (StringUtils.isBlank(keywords)) {
            return HttpResult.errorMsg(null);
        }
        if (page == null) {
            page = COMMENT_PAGE;
        }
        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }
        PagedGridResult result = itemsService.searchItems(keywords, sort, page, pageSize);
        return HttpResult.ok(result);
    }

    @ApiOperation(value = "根据商品分类搜索商品列表", notes = "根据商品分类搜索商品列表", httpMethod = "GET")
    @GetMapping("/catItems")
    public HttpResult catItems(
            @ApiParam(name = "catId", value = "商品三级分类id", required = true) @RequestParam Integer catId,
            @ApiParam(name = "sort", value = "排序规则", required = false) @RequestParam String sort,
            @ApiParam(name = "page", value = "查询第几页", required = false) @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页每一页显示的条数", required = false) @RequestParam Integer pageSize) {
        if (catId == null) {
            return HttpResult.errorMsg(null);
        }
        if (page == null) {
            page = COMMENT_PAGE;
        }
        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }
        PagedGridResult result = itemsService.searchItemsByThirdCat(catId, sort, page, pageSize);
        return HttpResult.ok(result);
    }


    // 用于用户长时间未登录网站，刷新购物车中的数据(主要是商品价格)类似京东淘宝
    @ApiOperation(value = "根据商品规格ids查找最新的商品数据", notes = "根据商品规格ids查找最新的商品数据", httpMethod = "GET")
    @GetMapping("/refresh")
    public HttpResult refresh(
            @ApiParam(name = "itemSpecIds", value = "拼接的规格ids", required = true, example = "1001,1003,1005") @RequestParam String itemSpecIds) {
        if (StringUtils.isBlank(itemSpecIds) ) {
            return HttpResult.errorMsg(null);
        }
        List<ShopCartVO> shopCartVOS = itemsService.queryItemsBySpecIds(itemSpecIds);
        return HttpResult.ok(shopCartVOS);
    }
}
